package com.future.direction.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.container.MeituanFooter;
import com.liaoinstan.springview.container.MeituanHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.future.direction.R;
import com.future.direction.di.component.AppComponent;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xwl on 2019/11/06
 * 搜索页面
 */
public class SearchActivity extends BaseActivity implements View.OnClickListener {


    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_cancle)
    TextView tvCancle;

    @BindView(R.id.ll_recent_searches)
    LinearLayout llRecentSearches;

    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.tabLayout)
    TagFlowLayout tabLayout;

    private TagAdapter<String> tagAdapter;
    private ArrayList<String> mSearches;


    @Override
    public int setLayout() {
        return R.layout.activity_search;
    }

    @Override
    public void getExtras() {

    }

    @Override
    public boolean init() {

        springView.setHeader(new MeituanHeader(this));
        springView.setFooter(new DefaultFooter(this));

        iniTrecentSearches();
        return false;

    }

    private void iniTrecentSearches() {
        mSearches = new ArrayList<>();
        mSearches.add("商业");
        mSearches.add("股权");
        mSearches.add("企业文化");
        mSearches.add("营销");
        mSearches.add("创新");
        mSearches.add("管理");
        mSearches.add("创业");
        mSearches.add("境界");
        tagAdapter = new TagAdapter<String>(mSearches) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView textView = (TextView) getLayoutInflater().inflate(R.layout.item_tag_recent_searches_text, null);
                textView.setText(s);
                return textView;
            }
        };
        tabLayout.setAdapter(tagAdapter);
    }


    @Override
    public void setListener() {
        //取消
        tvCancle.setOnClickListener( this);
        //最近搜索标签点击
        tabLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                String searchText = mSearches.get(position);
                etSearch.setText(searchText);
                etSearch.setSelection(searchText.length());//将光标移至文字末尾
                return false;
            }
        });
        //搜索框的输入监听
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length()>0){
                    llRecentSearches.setVisibility(View.GONE);
                }else {
                    llRecentSearches.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //键盘上搜索键的点击
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索的时候隐藏软键盘
                    InputMethodManager manager = (InputMethodManager) etSearch.getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    manager.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
                    // 在这里写搜索的操作,一般都是网络请求数据
                    if (etSearch.getText().toString().length()>0){

                    }else {
                        showError("请您先输入要搜索的内容");
                    }

                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancle://取消
                finish();
                break;

        }
    }
}
